<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    function arr(nums, k) {
      let set = new Set(nums), sum = (1 + k) * k / 2, next = k + 1
      for (let n of set) {
        if (n <= k) {
          while (set.has(next)) next++
          sum += next - n, next++
        }
      }
      return sum
    };
    console.log(arr([1, 4, 25, 10, 25], 2));
  </script>
</body>

</html>